#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:taotieren


"""
    rpi.gpio 需要 root 权限，板上运行需要使用
    sudo python switch_2_pos.py
    通过拨动开关切换开关状态
    拨动开关上下波动，就会输出对应信息
    两个 pin 启用上拉电阻输入
    DPDT, SPDT, SPST, SPST
    D: 双
    S：单
    P：刀
    T：掷
"""

""" 非树莓派环境下可以用 rpi.gpio_def 代替 rpi.gpio 来进行程序调试和验证 """
import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)


top_pin = 18
bottom_pin = 23


GPIO.setup(top_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(bottom_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)


switch_position = "unknown"

while True:
    top_state = GPIO.input(top_pin)
    bottom_state = GPIO.input(bottom_pin)
    new_switch_position = "unknown"
    if top_state == False:
        new_switch_position = "UP"
    elif bottom_state == False:
        new_switch_position = "DOWN"
    else:
        new_switch_position = "CENTER"
    if new_switch_position != switch_position:
        switch_position = new_switch_position
        print(switch_position)



















